package com.example.cuilei.shell;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import java.io.DataOutputStream;
import java.io.OutputStream;


public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }

    public void exe(View view) {
        execShell(new String[]{
                "chmod 666 /sys/class/leds/button-backlight/brightness",
                "echo 0 >/sys/class/leds/button-backlight/brightness",
                "chmod 444 /sys/class/leds/button-backlight/brightness"
        });
    }

    public void execShell(String cmd) {
        execShell(new String[]{cmd});
    }

    public void execShell(String[] cmds) {
        try {
            //权限设置
            Process p = Runtime.getRuntime().exec("su");
            //获取输出流
            OutputStream outputStream = p.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            //将命令写入
            StringBuffer sbcmd = new StringBuffer();
            for (String cmd : cmds) {
                sbcmd.append(cmd + ";");
            }
            dataOutputStream.writeBytes(sbcmd.toString());
            //提交命令
            dataOutputStream.flush();
            //关闭流操作
            dataOutputStream.close();
            outputStream.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
